package com.lagou.minicat.core;

import org.dom4j.Document;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

public class Host {

    private String name;
    private String appBase;

    private List<Context> contexts = new ArrayList<>();

    public Host(String name, String appBase) {
        this.name = name;
        this.appBase = appBase;
    }

    public void init(Document serverConfigDoc) {
        Element rootElement = serverConfigDoc.getRootElement();
        // 查找Context
        List<Element> contextNodes = rootElement.selectNodes("/Server/Service/Engine/Host/Context");
        contextNodes.forEach(c -> {
            String path = c.attributeValue("path");
            String docBase = c.attributeValue("docBase");
            Context context = new Context(path, docBase, appBase);
            context.init();
            contexts.add(context);
        });
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAppBase() {
        return appBase;
    }

    public void setAppBase(String appBase) {
        this.appBase = appBase;
    }

    public List<Context> getContexts() {
        return contexts;
    }

    public void setContexts(List<Context> contexts) {
        this.contexts = contexts;
    }
}
